<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>Full Layout - jQuery EasyUI Demo</title>
	<#include "./../common/header.html"/>
</head>
<body class="easyui-layout">
	<table id="dg" class="easyui-datagrid" title="开关配置管理" style="width:100%;height:98%"
		data-options="
				iconCls: 'icon-ok',
				singleSelect: true,
				url:'${ctx}/switch/loadAllMemeKeys',
				toolbar: '#tb',
				pageSize:1000,
				pagination:true,
				pageNumber:1,
			">
		<thead>
			<tr>
				<th data-options="field:'title',width:250">描述</th>
				<th data-options="field:'memKey',width:300">key</th>
				<th data-options="field:'memValue',width:250">缓存中的值</th>
				<th data-options="field:'type',width:150">类型</th>
				<th data-options="field:'value',width:150">数据库中的值</th>
				<th data-options="field:'enable',width:50">是否启用</th>
			</tr>
		</thead>
	</table>

	<div id="tb" style="height:auto">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add_switch()">添加开关</a>
		<!--<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="deleteRows()">删除</a>-->
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editRows()">修改</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="activeRows()">激活/关闭</a>
	</div>

	<div id="add_dlg" class="easyui-dialog" closed="true" data-options="width:700,height:500">
		<div id="content">
		
			<div style="padding:10px 60px 20px 60px">
	    <form id="ff" method="post" action="${ctx}/switch/addSwitch">
	    	<table cellpadding="5">
	    		<tr>
	    			<td>描述:</td>
	    			<td><input class="easyui-textbox" type="text" name="title" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td>key:</td>
	    			<input type="hidden" name="_id" />
	    			<td><input class="easyui-textbox" type="text" name="memKey" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td>类型：</td>
	    			<td><select id="cc" class="easyui-combobox" name="type" data-options="required:true" style="width:200px;">
					    <option value="">请选择</option>
					    <option value="counter">计数器(value>0) </option>
					    <option value="switch">开关(value=-1 or value>-1)</option>
					</select></td>
	    		</tr>
	    		<tr>
	    			<td>数据库中的配置值:</td>
	    			<td><input type="text" name="value" class="easyui-numberbox" data-options="required:true" value="" data-options="min:0,precision:0"/></td>
	    		</tr>
	    		<tr>
	    			<td>是否启用:</td>
	    			<td><input name="enable" type="radio" value="true"/>是<input name="enable" type="radio" value="false"/>否</td>
	    		</tr>
	    	</table>
	    </form>
		</div>
	</div>

</body>
<script>

function deleteRows(){
	var row = $("#dg").datagrid('getSelected'); 
	if(row!=null){
		$.messager.confirm('删除?', '确认删除?', function(r){
			if(r){
				$.ajax({ url: "${ctx}/switch/deleteMemConfig?id="+row._id, success: function(response){
					var data= $.parseJSON(response); 
					if(response==true){
						$("#dg").datagrid('reload');
					}
					$.messager.alert({
						title : '提示',
						msg : "操作成功"
					});
				}});
			}

		});
	
	} else {
		$.messager.alert({
			title : '提示',
			msg : "请选择一行"
		});
	}
}

function activeRows(){
	var row = $("#dg").datagrid('getSelected'); 
	if(row!=null){
		var doEnable = (row.enable == "false");
		var title = doEnable ? "激活" : "关闭";
		$.messager.confirm(title, '确认'+title+'?', function(r){
			if(r){
				$.ajax({ url: "${ctx}/switch/activeMemConfig?id="+row._id+"&enable=" + doEnable, success: function(response){
					var data= $.parseJSON(response); 
					if(response==true){
						$("#dg").datagrid('reload');
					}
					$.messager.alert({
						title : '提示',
						msg : "操作成功"
					});
				}});
			}

		});
	
	} else {
		$.messager.alert({
			title : '提示',
			msg : "请选择一行"
		});
	}
}

function editRows(){
	var row = $("#dg").datagrid('getSelected'); 
	if(row!=null){
		 $('#ff').form('load',row);
		add_switch();
	
	} else {
		$.messager.alert({
			title : '提示',
			msg : "请选择一行"
		});
	}
}
function add_switch(){
	$('#add_dlg').dialog({
		title:'添加开关',
		closed: false,
		cache: false,
		buttons:[{ 
			text:'提交', 
			iconCls:'icon-ok',
		
			handler:function(){
				$("#add_dlg").find('#ff').form('submit',{
					onSubmit:function(){
					var flag = $(this).form('validate');
						return flag;
					},
					dataType:"json",
					success : function(response) {
						if(response=='true'){
							$.messager.alert({
								title : '提示',
								msg : "操作成功"
							});
						}
					
						$('#add_dlg').dialog('close');
						$('#dg').datagrid('reload'); 
					}
				});
				
			} 
		},{ 
			text:'关闭', 
			handler:function(){ 
				$('#add_dlg').dialog('close');
			} 
		}], 
		onOpen:function(){ 
			//$('#add_contentt_dlg_{$rbtId}').dialog('refresh', '__URL__/toAddInfoForm&rbtId={$rbtId}'); 
		} 
	});
	
	
}
</script>
</html>